package com.duobaoyu.dby.supply.db.config;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.DynamicTableNameInnerInterceptor;
import com.duobaoyu.dby.common.core.constant.StringPool;
import com.duobaoyu.dby.supply.db.constant.SupplyMapperConstant;
import com.duobaoyu.dby.supply.db.helper.DynamicTableNameHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Bean;

/**
 * @author liyang
 * @version 1.0.0
 * @ClassName MybatisPlusConfig
 * @createTime 09:18:00 2023/09/27
 */
//@Configuration
public class MybatisPlusConfig {
	@Bean
	public MybatisPlusInterceptor mybatisPlusInterceptor() {
		MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
		DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor();
		dynamicTableNameInnerInterceptor.setTableNameHandler((sql, tableName) -> {
			// 获取参数方法
			String suffix = DynamicTableNameHelper.getRequestData(tableName);
			if (SupplyMapperConstant.dynamicTableNames().contains(tableName) && StringUtils.isNotBlank(suffix)) {
				return tableName.concat(StringPool.UNDERLINE).concat(suffix);
			}
			return tableName;
		});
		interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor);
		// 3.4.3.2 作废该方式
		// dynamicTableNameInnerInterceptor.setTableNameHandlerMap(map);
		return interceptor;
	}
}
